package cn.com.joco.dk.common;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;

import cn.com.joco.dk.core.user.DesUtil;
import cn.com.joco.dk.core.user.MultiUsernamePasswordToken;

/**
 * Servlet Filter implementation class AutoLoginFilter
 */
public class AutoLoginFilter implements Filter {
	public static ThreadLocal<String> basePath = new ThreadLocal<String>();
    /**
     * Default constructor. 
     */
    public AutoLoginFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
		basePath.remove();
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		Subject subject = SecurityUtils.getSubject();
		HttpServletRequest httpRequet = (HttpServletRequest)request;
		String url=httpRequet.getRequestURI();
		HttpServletRequest httpServletRequest = (HttpServletRequest)request;
		String path = httpServletRequest.getScheme()+"://"+request.getServerName()+":" + request.getServerPort();
		basePath.set(path);
		if(url.endsWith("rest/phone/registerCheck")
				||url.endsWith("rest/phone/verify")
				||url.endsWith("rest/phone/initPsw")
				||url.endsWith("/ws/rest/phone/register")
				||url.endsWith("/ws/rest/phone/getJsessionId")
				||url.endsWith("/ws/rest/phone/getRegisterProtocol")
				||url.endsWith("/ws/rest/phone/getApplication")
				||url.endsWith("/ws/rest/phone/getHelpList")
				||url.endsWith("/ws/rest/phone/getMessgaeList")
				){
			chain.doFilter(request, response);
			return;
		}
		String token=request.getParameter("token");
		if(!subject.isAuthenticated()){
				if(token==null){
					throw new RuntimeException("未经过认证");
				}
				String username="";
				String password="";
				String firstLoginTime="";
				try {

					DesUtil desUtil=new DesUtil("abcd*&^cbv+++~`7ddaw");
					String[] securitys=desUtil.decrypt(token).split(":");
					if(securitys.length==3){
						username=securitys[0];
						password=securitys[2];
						firstLoginTime=securitys[1];
					}else{
						throw new RuntimeException("令牌错误");
					}
				} catch (Exception e) {
					throw new RuntimeException(e);
				}
				UsernamePasswordToken uToken= new UsernamePasswordToken(username, password);
				String module="member";
				uToken=new MultiUsernamePasswordToken(uToken.getUsername(), uToken.getPassword(),uToken.isRememberMe(),module,null);
				
				subject.login(uToken);
				

				chain.doFilter(request, response);
			
		}else{
			chain.doFilter(request, response);
		}
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}
	
}
